<template>
  <ele-page>
    <!-- 搜索表单 -->
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        :pagination="false"
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>

        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>

        <template #groupTitle="{ row }">
          <el-select
            v-model="row.tenant_group_id"
            class="mr"
            style="width: 150px"
            @change="(val) => updateTenantGroup(row, val)"
          >
            <el-option :value="0" label="无">无</el-option>
            <el-option
              v-for="item in tenantGroupList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </template>

        <template #images="{ row }">
          <span class="pointer blue" @click="openImages(row)"> 查看 </span>
        </template>

        <template #balance="{ row }">
          <span class="pointer blue" @click="openBalanceDetail(row)">
            {{ row.balance }}
          </span>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="info" size="small" @click="openEdit(row)">
              编辑
            </el-button>
            <!-- <el-button type="danger" size="small" @click="del(row)">
              删除
            </el-button> -->
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
    <BalanceDetail v-model="showBalanceDetail" :tenant-id="currentId" />
  </ele-page>
</template>

<script setup>
  import { ref, computed, onMounted } from 'vue';

  import Edit from './edit.vue';
  import BalanceDetail from './balance-detail.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import {
    pageTenant,
    pageTenantGroup,
    updateTenantTenantGroup
  } from '@/api/company';
  import { Plus } from '@element-plus/icons-vue';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 80,
        align: 'center'
      },
      {
        slot: 'groupTitle',
        label: '所属集团',
        width: 180
      },
      {
        prop: 'title',
        label: '简称'
      },
      {
        prop: 'full_title',
        label: '全名'
      },
      {
        prop: 'credit',
        label: '信用额度',
        width: 125
      },
      {
        slot: 'balance',
        label: '账号余额',
        width: 125
      },
      {
        prop: 'canUse',
        label: '可用额度',
        width: 125
      }
    );

    cols.push({
      columnKey: 'action',
      label: '操作',
      width: 80,
      slot: 'action',
      align: 'center',
      fixed: 'right'
    });

    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, where, orders }) => {
    return pageTenant({
      ...where,
      ...orders,
      page,
      limit: 9999
    });
  };

  const showBalanceDetail = ref(false);
  const openBalanceDetail = (row) => {
    showBalanceDetail.value = true;
    currentId.value = row.id;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };
  const tenantGroupList = ref([]);
  const getTenantGroup = () => {
    const loading = EleMessage.loading({ message: '更新中...', mask: true });
    pageTenantGroup({
      page: 1,
      limit: 9999
    })
      .then((res) => {
        loading.close();
        tenantGroupList.value = res.list;
      })
      .catch((e) => {
        console.log('出错', e);
        loading.close();
      });
  };

  const updateTenantGroup = (row, newGroupId) => {
    console.log('当前行数据：', row);
    console.log('新的分组ID：', newGroupId);

    // 例如发请求更新
    updateTenantTenantGroup({
      id: row.id,
      tenant_group_id: newGroupId
    }).then(() => {
      EleMessage.success('更新成功');
      reload();
    });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  onMounted(() => {
    getTenantGroup();
  });
</script>
